package com.xoa.util.redis;

import com.xoa.model.users.Users;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionException;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/31.
 */
public class RedisSessionUtil {

    public static HashMap getUserSessionInfo(String sessionId, JedisUtil.Strings strings) {
        if (sessionId == null) {
            throw new SessionException("session is null");
        }
        byte[] usermapByte = strings.get(SerializeUtil.serialize(sessionId));
        if (usermapByte != null) {
            strings.setEx(SerializeUtil.serialize(sessionId),1800, usermapByte);
            HashMap usersMap = (HashMap) SerializeUtil.unserialize(usermapByte);
            return usersMap;
        }


        return null;
    }

    public static HashMap setUserSessionInfo(String sessionId, JedisUtil.Strings strings, Map userSessionInfo) {
        if (sessionId == null) {
            throw new SessionException("session is null");
        }

        HashMap usersMap = (HashMap) SerializeUtil.unserialize(strings.get(SerializeUtil.serialize(sessionId)));
        if (usersMap != null) {
            strings.setEx(SerializeUtil.serialize(sessionId),1800, SerializeUtil.serialize(userSessionInfo));

        }
        return usersMap;
    }


    public static void initUserSession(String sessionId, JedisUtil.Strings strings, Users user) {
        if (sessionId == null)
            return;

        if (strings.get(SerializeUtil.serialize(sessionId)) == null) {
            HashMap userSessionInfo = new HashMap();
            userSessionInfo.put("uid", user.getUid());
            userSessionInfo.put("userId", user.getUserId());
            userSessionInfo.put("userName", user.getUserName());
            userSessionInfo.put("deptId", user.getDeptId());
            userSessionInfo.put("userPriv", user.getUserPriv());
            userSessionInfo.put("theme", user.getTheme());
            userSessionInfo.put("notLogin", user.getNotLogin());
            userSessionInfo.put("password", user.getPassword());
            strings.setEx(SerializeUtil.serialize(sessionId),1800, SerializeUtil.serialize(userSessionInfo));
        } else {
            HashMap userSessionInfo = (HashMap) SerializeUtil.unserialize(strings.get(SerializeUtil.serialize(sessionId)));
            userSessionInfo.put("uid", user.getUid());
            userSessionInfo.put("userId", user.getUserId());
            userSessionInfo.put("userName", user.getUserName());
            userSessionInfo.put("deptId", user.getDeptId());
            userSessionInfo.put("userPriv", user.getUserPriv());
            userSessionInfo.put("theme", user.getTheme());
            userSessionInfo.put("notLogin", user.getNotLogin());
            userSessionInfo.put("password", user.getPassword());
            strings.setEx(SerializeUtil.serialize(sessionId),1800, SerializeUtil.serialize(userSessionInfo));


        }

    }

}
